package jmx.study.mbean.notification;

import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;

import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;

/**
 * MBean 接入层
 *
 */
public class NotifiHelloAgent {

	public static void main(String[] args)  throws Exception {
		MBeanServer server = ManagementFactory.getPlatformMBeanServer();
		ObjectName helloName = new ObjectName("jmxstudy:name=notifyHello");
		server.registerMBean(new NotifiHello(), helloName);

		try {
			// 注册一个端口，绑定url后用于客户端通过rmi方式连接JMXConnectorServer
			LocateRegistry.createRegistry(9999);
			// URL路径
			JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://localhost:9999/jndi/rmi://localhost:9999/jmxDefault");
			JMXConnectorServer jcs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, server);
			System.out.println("NotifiHelloAgent start...");
			jcs.start();
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


}
